package com.chunlin.set;

import java.util.Arrays;
import java.util.HashSet;

public class HashSetTest {
    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        HashSet<String> names = new HashSet<>();

        // 添加元素
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("Diana");

        // 检查是否包含元素
        System.out.println("Contains 'Alice': " + names.contains("Alice"));

        // 删除元素
        names.remove("Alice");
        System.out.println("After removing 'Alice': " + names);

        // 使用批量操作
        HashSet<String> newNames = new HashSet<>(Arrays.asList("Eve", "Frank"));
        names.addAll(newNames);
        System.out.println("After adding new names: " + names);

        // 遍历元素
        for (String name : names) {
            System.out.println("Name: " + name);
        }

        // 清空集合
        names.clear();
        System.out.println("After clearing: " + names);
    }
}
